home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr47 / sb16snd.zip / SBPLAY.C < prev    next >
C/C++ Source or Header  |  1995-02-18  |  4KB  |  178 lines

  1. /*           Copyright 1995 by Ethan Brodsky.  All rights reserved          */
  2.  
  3. /* sbplay.c */
  4.  
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <mem.h>
  10.  
  11. #include "sbio.h"
  12. #include "xms.h"
  13.  
  14. #define BASEIO 0x220
  15. #define IRQ    5
  16. #define DMA16  5
  17.  
  18. #define LOAD_CHUNK_SIZE 8192
  19. #define BLOCK_LENGTH    256
  20.  
  21. #define FALSE 0
  22. #define TRUE  1
  23.  
  24. int (far *bufptr)[2][BLOCK_LENGTH] = NULL;
  25.  
  26. unsigned int rate;
  27. char filename[64] = "";
  28.  
  29. long numsamples;
  30.  
  31. int  handle;
  32. long cursample;
  33.  
  34. MOVEPARAMS moveparams;
  35.  
  36. int getparameters
  37.  (int argc, char *argv[], unsigned int *rate, char *fname)
  38.   {
  39.     if (argc != 3) /* The program name counts as an argument */
  40.       return FALSE;
  41.     else
  42.       {
  43.         if ((*rate = atoi(argv[1])) == 0) return FALSE;
  44.         strcpy(fname, argv[2]);
  45.         return TRUE;
  46.       }
  47.   }
  48.  
  49. void loaddata(void)
  50.   {
  51.     FILE *f;
  52.     char chunk[LOAD_CHUNK_SIZE];
  53.     long size;
  54.  
  55.     if ((f = fopen(filename, "rb")) == NULL)
  56.       {
  57.         printf("Error opening %s!\n", *filename);
  58.         exit(EXIT_FAILURE);
  59.       }
  60.  
  61.     fseek(f, 0, SEEK_END); /* Move to end of file */
  62.     size = ftell(f);       /* File size = end pos */
  63.     fseek(f, 0, SEEK_SET); /* Back to begining    */
  64.     numsamples = size / 2; /* 16-bit samples      */
  65.  
  66.     xms_init();
  67.     if (!xms_allocate(&handle, (unsigned int)((size / 1024) + 1)))
  68.       {
  69.         printf("ERROR:  Not enough free XMS!\n");
  70.         printf("        Bytes required:  %u\n", size);
  71.         printf("        Bytes free:      %u\n", xms_getfreemem()*1024);
  72.         exit(EXIT_FAILURE);
  73.       }
  74.     else
  75.       {
  76.         moveparams.sourcehandle = 0;
  77.         moveparams.sourceoffset = (long)(&chunk);
  78.         moveparams.desthandle   = handle;
  79.         moveparams.destoffset   = 0;
  80.  
  81.         do
  82.           {
  83.             moveparams.length = fread(&chunk, 1, LOAD_CHUNK_SIZE, f);
  84.             xms_move(&moveparams);
  85.             moveparams.destoffset += moveparams.length;
  86.             size -= moveparams.length;
  87.           }
  88.         while (size > 0);
  89.  
  90.         fclose(f);
  91.       }
  92.   }
  93.  
  94. void copyblock(char blocknum)
  95.   {
  96.     int length;
  97.  
  98.     if ((cursample + BLOCK_LENGTH) <= numsamples)
  99.       length = BLOCK_LENGTH*2;
  100.     else
  101.       {
  102.         length = (int)(numsamples-cursample) * 2;
  103.         memset((void *)(&(*bufptr)[blocknum][length/2]), 0x00, (BLOCK_LENGTH-(length/2)) * 2);
  104.       }
  105.     moveparams.length       = length;
  106.     moveparams.sourcehandle = handle;
  107.     moveparams.sourceoffset = cursample*2;
  108.     moveparams.desthandle   = 0;
  109.     moveparams.destoffset   = (long)((*bufptr)[blocknum]);
  110.  
  111.     xms_move(&moveparams);
  112.  
  113.     cursample += BLOCK_LENGTH;
  114.   }
  115.  
  116. void far playhandler(void)
  117.   {
  118.     if (cursample < numsamples)
  119.       copyblock(curblock);
  120.     else
  121.       memset((void *)((*bufptr)[curblock]), 0x00, BLOCK_LENGTH*2);
  122.   }
  123.  
  124. void init(void)
  125.   {
  126.     loaddata();
  127.     getbuffer(&(int far *)bufptr, BLOCK_LENGTH);
  128.  
  129.     cursample = 0;
  130.  
  131.     memset((void *)bufptr, 0xFF, sizeof(*bufptr));
  132.     copyblock(0);
  133.     copyblock(1);
  134.  
  135.     sethandler(playhandler);
  136.     init_sb(BASEIO, IRQ, DMA16, output, rate);
  137.     startio(numsamples);
  138.   }
  139.  
  140. void shutdown(void)
  141.   {
  142.     shutdown_sb();
  143.     sethandler(NULL);
  144.     freebuffer(&(int far *)bufptr);
  145.  
  146.     xms_free(&handle);
  147.   }
  148.  
  149. int main(int argc, char *argv[])
  150.   {
  151.     printf("SBPLAY - Copyright 1995 by Ethan Brodsky.  All rights reserved\n");
  152.  
  153.     if (getparameters(argc, argv, &rate, filename))
  154.       printf("Playing %s at %u HZ\n", filename, rate);
  155.     else
  156.       {
  157.         printf("Syntax:  sbrecord <rate> <filename>\n");
  158.         printf("Example: sbrecord 22050 data.raw\n");
  159.         exit(EXIT_FAILURE);
  160.       }
  161.  
  162.     init();
  163.  
  164.     while (!(done || kbhit()));
  165.  
  166.     if (kbhit())
  167.       {
  168.         printf("Output terminated by keypress\n");
  169.         getch();
  170.       }
  171.  
  172.     shutdown();
  173.  
  174.     printf("\n");
  175.  
  176.     return(EXIT_SUCCESS);
  177.   }
  178.